package com.sleep.myutilsspringbootstarter.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @ClassName R
 * @Description TODO
 * @Author 郭敖
 * @Date 2024年08月16日 16:01
 * @Version 1.0
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class R implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private String code;

    private String msg;

    private Object data;

    private static final String SUCCESS = "200";

    public static <T> R func(T data, Function<T,Object> function){
        R res = new R();
        res.setData(function.apply(data));
        return res;
    }

    /**
     * @Describe: 对结果集的数据进行自定义的处理
     * @author 郭敖
     * @date 16:13 2024/8/16
     * @param function 函数式处理接口
     * @return R 当前处理对象
     */
    public R func(Function<R,Object> function){
        this.setData(function.apply(this));
        return this;
    }

    public static  R success(Object data){
        return new R().setCode(SUCCESS).setData(data).setMsg("操作成功");
    }

}
